package com.cocostudio.erpapp.view;

import android.os.Bundle;
import androidx.fragment.app.ListFragment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.Button;
import android.widget.ListView;

import com.cocostudio.erpapp.common.Debug;
import com.cocostudio.erpapp.R;

public abstract class MultiPageListFragment extends ListFragment implements OnScrollListener, OnClickListener {
    private static final String TAG = MultiPageListFragment.class.getSimpleName();
    private int mPageCount = 1;
    private int mCurrentPage = 1;
    private int visibleLastIndex = 0; // 最后的可视项索引
    private int visibleItemCount; // 当前窗口可见项总数
    private View mFooterView;
    private Button mBtnLoadMore;
    private boolean mFooterViewVisible = true;

    protected abstract void onLoadMore(int page);

    /**
     * TODO will perform abnormal when can't access network.
     * 
     * @param count
     */
    protected void setPageCount(int count) {
        mPageCount = count;
    }

    protected void setLoadMoreVisible(boolean visible) {
        if (Debug.LOGD) {
            Log.d(TAG, "setLoadMoreVisible. visible=" + visible);
        }
        if (mFooterView != null) {
            ListView listView = getListView();
            if (visible) {
                if (!mFooterViewVisible) {
                    listView.addFooterView(mFooterView);
                    mFooterViewVisible = true;
                }
            } else {
                if (mFooterViewVisible) {
                    listView.removeFooterView(mFooterView);
                    mFooterViewVisible = false;
                }
            }
        }
    }   

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getListView().setOnScrollListener(this);
        mFooterView = getActivity().getLayoutInflater().inflate(R.layout.item_load_more, null);
        mBtnLoadMore = (Button) mFooterView.findViewById(R.id.btn_loadmore);
        mBtnLoadMore.setOnClickListener(this);
        //暂时隐藏加载更多按钮
        mBtnLoadMore.setVisibility(View.GONE);
        getListView().addFooterView(mFooterView);
    }

    @Override
    public void onClick(View v) {
        mBtnLoadMore.setText(R.string.label_loading); // 设置按钮文字loading
        onLoadMore(++mCurrentPage);
    }

    protected void finishLoadMore() {
        getListView().setSelection(visibleLastIndex - visibleItemCount + 1); // 设置选中项
        mBtnLoadMore.setText(R.string.label_load_more); // 恢复按钮文字
        checkNeedLoadMore();
    }

    private void checkNeedLoadMore() {
        if (mCurrentPage >= mPageCount) {
            setLoadMoreVisible(false);
        } else {
            setLoadMoreVisible(true);
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        this.visibleItemCount = visibleItemCount;
        visibleLastIndex = firstVisibleItem + visibleItemCount - 1;
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        int itemsLastIndex = getListAdapter().getCount() - 1; // 数据集最后一项的索引
        int lastIndex = itemsLastIndex + 1; // 加上底部的loadMoreView项
        if (scrollState == OnScrollListener.SCROLL_STATE_IDLE && visibleLastIndex == lastIndex) {
            // 如果是自动加载,可以在这里放置异步加载数据的代码
        }
    }
}
